import 'package:acgn_pioneer/components/book-item/book1.dart';
import 'package:acgn_pioneer/components/circle_tag.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

import '../../public.dart';
import 'mixins/widget_base.dart';

/// 阅读历史记录
class ReaderHistory extends StatefulWidget {
  ReaderHistory({Key key}) : super(key: key);

  @override
  ReaderHistoryState createState() => new ReaderHistoryState();
}

class ReaderHistoryState extends State<ReaderHistory> with ReaderWidgetBase {
  UserRecordProvider userRecord;
  int currentIndex = 0;

  @override
  void initState() {
    super.initState();
    providerHanddle();
  }

  void providerHanddle() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      userRecord.setUserRecord();
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 使用Provider
    userRecord = Provider.of<UserRecordProvider>(context);

    return Container(
        padding: EdgeInsets.only(left: 16, right: 16),
        child: Column(children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                '阅读历史',
                style: TextStyle(color: Colors.white, fontSize: 16),
              ),
              SizedBox(width: 10),
              Text(
                '仅保留最近50条的阅读记录',
                style: TextStyle(
                    color: ColorUtil.rgba(255, 255, 255, 0.4), fontSize: 14),
              )
            ],
          ),
          new SizedBox(height: 16),
          (userRecord != null && userRecord.userRead.length != 0)
              ? _buildList()
              : EmptyComponent(
                  desc: '暂无阅读历史', imageAsset: 'assets/images/empty_yuedu.png')
        ]));
  }

  Widget _buildList() {
    return SizedBox(
        height: Rem(220),
        width: double.infinity,
        child: new Swiper(
          loop: userRecord.userRead.length >= 3,
          index: currentIndex,
          itemBuilder: (BuildContext context, int index) {
            UserReadItemModel model = userRecord.userRead[index];
            return Container(
                child: Stack(children: [
              Book1Component(
                  cover: model.cover3x4,
                  imgSize: Size(113, 150),
                  title: model.comicName,
                  desc: model.chapterName),
              Positioned(
                child: CircleTag(
                    text: (index + 1).toString() +
                        '/' +
                        userRecord.userRead.length.toString(),
                    textColor: ColorUtil.rgba(51, 51, 51, 1),
                    bgColor: ColorUtil.rgba(255, 215, 59, 1)),
              )
            ]));
          },
          scale: 0.5,
          fade: 0,
          viewportFraction: 0.35,
          itemCount: userRecord.userRead.length,
        ));
  }
}
